package jmine.tec.proxy.chain;

/**
 * {@link ChainElement} que faz um shortcut com os metodos
 * {@link Object#equals(Object)} e {@link Object#hashCode()}
 * 
 * @author takeshi
 * 
 */
public class EqualsHashCodeChainElement implements ChainElement {

    /**
     * Continua o chain se o metodo nao eh nem o equals nem o hashCode.
     * 
     * @param chain
     *            o invocationChain
     * @return o objeto a ser devolvido
     * @throws Throwable
     *             se uma excecao for lancada
     */
    public Object aroundInvoke(final InvocationChain chain) throws Throwable {
        if (chain.getInvokingMethod().getDeclaringClass() == Object.class) {
            String methodName = chain.getInvokingMethod().getName();
            if (methodName.equals("equals")) {
                return chain.getProxyObject() == chain.getParams()[0];
            } else if (methodName.equals("hashCode")) {
                return System.identityHashCode(chain.getProxyObject());
            }
        }
        return chain.proceed();
    }

}
